package com.vlmap.security.oauth2.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
public class TestEndpoints {


    @Autowired
    ClientDetailsService clientDetailsService;

    @GetMapping("/user/info")
    @ResponseBody
    public Object userInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getPrincipal();

    }

    @ResponseBody
    @GetMapping("/client/info")
    public Object clientInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getDetails();
    }

    /**
     * 模拟第三方接入
     *
     * @param code
     * @param clientId
     * @param domain
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/code")
    @ResponseBody
    public Object authorization_code(@RequestParam("code") String code, @RequestParam("client_id") String clientId, @RequestParam(value = "domain", defaultValue = "http://localhost:8080") String domain, HttpServletRequest request, HttpServletResponse response) {


        List<String> list = new ArrayList<String>();
        list.add("grant_type=" + "authorization_code");
        list.add("client_id=" + clientId);
        list.add("code=" + code);
        list.add("client_secret=" + "123456");
        String queryString = request.getQueryString();
        /**
         * redirect_uri要和授权地址一致，参数也要加上
         */
        if (StringUtils.isBlank(queryString)) {
            list.add("redirect_uri=" + request.getRequestURL());
        } else {
            list.add("redirect_uri=" + request.getRequestURL() + "?" + queryString);

        }

        RestTemplate template = new RestTemplate();
        String host = request.getHeader("host");
        if (StringUtils.isBlank(host)) {
            host = "http://localhost:8080";
        } else {
            host = "http://" + host;
        }

        String tokenUrl = host + "/oauth/token?" + StringUtils.join(list, "&");
        Object o = template.getForObject(tokenUrl, HashMap.class);
        return o;


    }

    @GetMapping("/param")
    @ResponseBody
    public Object param(HttpServletRequest request, HttpServletResponse response) {
        return request.getParameterMap();
    }
}
